home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / env.el.z / env.el
Encoding:
Text File  |  1998-05-21  |  4.2 KB  |  117 lines

  1. ;;; env.el --- functions to manipulate environment variables.
  2.  
  3. ;; Copyright 1991, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: processes, unix
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; UNIX processes inherit a list of name-to-string associations from their
  30. ;; parents called their `environment'; these are commonly used to control
  31. ;; program options.  This package permits you to set environment variables
  32. ;; to be passed to any sub-process run under XEmacs.
  33.  
  34. ;;; Code:
  35.  
  36. ;; History list for environment variable names.
  37. (defvar read-envvar-name-history nil)
  38.  
  39. (defun read-envvar-name (prompt &optional mustmatch)
  40.   "Read environment variable name, prompting with PROMPT.
  41. Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
  42. If it is also not t, RET does not exit if it does non-null completion."
  43.   (completing-read prompt
  44.            (mapcar (function
  45.                 (lambda (enventry)
  46.                   (list (substring enventry 0
  47.                            (string-match "=" enventry)))))
  48.                process-environment)
  49.            nil mustmatch nil 'read-envvar-name-history))
  50.  
  51. ;; History list for VALUE argument to setenv.
  52. (defvar setenv-history nil)
  53.  
  54. ;;;###autoload
  55. (defun setenv (variable &optional value unset)
  56.   "Set the value of the environment variable named VARIABLE to VALUE.
  57. VARIABLE should be a string.  VALUE is optional; if not provided or is
  58. `nil', the environment variable VARIABLE will be removed.  
  59.  
  60. Interactively, a prefix argument means to unset the variable.
  61. Interactively, the current value (if any) of the variable
  62. appears at the front of the history list when you type in the new value.
  63.  
  64. This function works by modifying `process-environment'."
  65.   (interactive
  66.    (if current-prefix-arg
  67.        (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
  68.      (let* ((var (read-envvar-name "Set environment variable: " nil))
  69.         (oldval (getenv var))
  70.         newval
  71.         oldhist)
  72.        ;; Don't put the current value on the history
  73.        ;; if it is already there.
  74.        (if (equal oldval (car setenv-history))
  75.        (setq oldval nil))
  76.        ;; Now if OLDVAL is non-nil, we should add it to the history.
  77.        (if oldval
  78.        (setq setenv-history (cons oldval setenv-history)))
  79.        (setq oldhist setenv-history)
  80.        (setq newval (read-from-minibuffer (format "Set %s to value: " var)
  81.                       nil nil nil 'setenv-history))
  82.        ;; If we added the current value to the history, remove it.
  83.        ;; Note that read-from-minibuffer may have added the new value.
  84.        ;; Don't remove that!
  85.        (if oldval
  86.        (if (eq oldhist setenv-history)
  87.            (setq setenv-history (cdr setenv-history))
  88.          (setcdr setenv-history (cdr (cdr setenv-history)))))
  89.        ;; Here finally we specify the args to give call setenv with.
  90.        (list var newval))))
  91.   (if unset (setq value nil))
  92.   (if (string-match "=" variable)
  93.       (error "Environment variable name `%s' contains `='" variable)
  94.     (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
  95.       (case-fold-search nil)
  96.       (scan process-environment)
  97.       found)
  98.       (if (string-equal "TZ" variable)
  99.       (set-time-zone-rule value))
  100.       (while scan
  101.     (cond ((string-match pattern (car scan))
  102.            (setq found t)
  103.            (if (eq nil value)
  104.            (setq process-environment (delq (car scan) process-environment))
  105.          (setcar scan (concat variable "=" value)))
  106.            (setq scan nil)))
  107.     (setq scan (cdr scan)))
  108.       (or found
  109.       (if value
  110.           (setq process-environment
  111.             (cons (concat variable "=" value)
  112.               process-environment)))))))
  113.  
  114. (provide 'env)
  115.  
  116. ;;; env.el ends here
  117.